home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / malloc / memalign.c < prev    next >
C/C++ Source or Header  |  1994-04-20  |  2KB  |  62 lines

  1. /* Copyright (C) 1991, 1992, 1993 Free Software Foundation, Inc.
  2.  
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public License as
  5. published by the Free Software Foundation; either version 2 of the
  6. License, or (at your option) any later version.
  7.  
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11. Library General Public License for more details.
  12.  
  13. You should have received a copy of the GNU Library General Public
  14. License along with this library; see the file COPYING.LIB.  If
  15. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  16. Cambridge, MA 02139, USA.  */
  17.  
  18. #ifndef    _MALLOC_INTERNAL
  19. #define _MALLOC_INTERNAL
  20. #include <malloc.h>
  21. #endif
  22.  
  23. __ptr_t
  24. memalign (alignment, size)
  25.      size_t alignment;
  26.      size_t size;
  27. {
  28.   __ptr_t result;
  29.   unsigned long int adj;
  30.  
  31.   size = ((size + alignment - 1) / alignment) * alignment;
  32.  
  33.   result = malloc (size);
  34.   if (result == NULL)
  35.     return NULL;
  36.   adj = (unsigned long int) ((unsigned long int) ((char *) result -
  37.                           (char *) NULL)) % alignment;
  38.   if (adj != 0)
  39.     {
  40.       struct alignlist *l;
  41.       for (l = _aligned_blocks; l != NULL; l = l->next)
  42.     if (l->aligned == NULL)
  43.       /* This slot is free.  Use it.  */
  44.       break;
  45.       if (l == NULL)
  46.     {
  47.       l = (struct alignlist *) malloc (sizeof (struct alignlist));
  48.       if (l == NULL)
  49.         {
  50.           free (result);
  51.           return NULL;
  52.         }
  53.       l->next = _aligned_blocks;
  54.       _aligned_blocks = l;
  55.     }
  56.       l->exact = result;
  57.       result = l->aligned = (char *) result + alignment - adj;
  58.     }
  59.  
  60.   return result;
  61. }
  62.